//
//  LRXHelpMoreInfo.m
//  路人侠
//
//  Created by zhangxulong on 3/6/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXHelpMoreInfoView.h"
#import "LRXThing.h"
#import "LRXHelpMoreInfoCell.h"
#import "zxlCircleView.h"
@interface LRXHelpMoreInfoView() <UITableViewDataSource,UITableViewDelegate,UIDynamicAnimatorDelegate>
@property (nonatomic,weak) LRXHelpMeInfoModel * model;
@property (nonatomic,strong) UIDynamicAnimator * animator;
@end

@implementation LRXHelpMoreInfoView
- (UIDynamicAnimator *)animator{
    if (_animator == nil) {
        _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self];
    }
    return _animator;
}

+ (instancetype)layoutHelpMoreView:(LRXHelpMeInfoModel * )model
{
    return [[LRXHelpMoreInfoView alloc]initWithHelpMoreModel:model];
}
- (instancetype)initWithHelpMoreModel:(LRXHelpMeInfoModel *)model
{
    if (self = [super init]) {
        
        
        self.model = model;
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10 * hScale;

        //添加滚动视图
        [self addContentView];
    }
    return self;
}

- (void)setUserIcon:(UIImageView *)userIcon
{
    _userIcon = userIcon;
    _userIcon.frame = CGRectMake(0, 0, 90 * wScale, 90 * wScale);
    _userIcon.center = CGPointMake(80 * wScale, 0);
    _userIcon.layer.cornerRadius = 45 * wScale;
    _userIcon.layer.masksToBounds = YES;
    [self addSubview:userIcon];
}
- (void)addContentView
{
    
    UILabel * startTime = [[UILabel alloc]init];
    UILabel * endTime = [[UILabel alloc]init];
    UIImageView * jiantou = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"jiantou"]];
  
    
    startTime.text = [NSString stringWithFormat:@"%@",self.model.startTime];
    startTime.textColor = mainColor;
    endTime.text = [NSString stringWithFormat:@"%@",self.model.endTime];
    endTime.textColor = startTime.textColor;
    endTime.font = [UIFont systemFontOfSize:29 * hScale];
    startTime.font = endTime.font;
    
    startTime.frame = CGRectMake(190 * wScale, 57 * hScale, 0, 0);
    endTime.frame = CGRectMake(300 * wScale, 57 * hScale, 0, 0);
    [startTime sizeToFit]; [endTime sizeToFit];
    
    CGFloat jiantouX = (CGRectGetMinX(endTime.frame) + CGRectGetMaxX(startTime.frame)) * 0.5 - 15 * hScale;
  
    jiantou.frame = CGRectMake(jiantouX, 65 * hScale, 30 * hScale, 16 * hScale);
    
    
    //设置tableView的内容
    UITableView * contentTableView = [[UITableView alloc]init];
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;   //没有分界线
    contentTableView.frame = CGRectMake(90 * wScale, 116 * hScale, 370 * wScale, 340 * hScale);
    
    contentTableView.backgroundColor = [UIColor clearColor];
    contentTableView.rowHeight = 60 * hScale;
    contentTableView.showsVerticalScrollIndicator = NO;
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    if (![startTime.text isEqualToString:@"(null)"] && ![endTime.text isEqualToString:@"(null)"]) {
        [self addSubview:startTime];
        [self addSubview:endTime];
        [self addSubview:jiantou];
    }

    
    [self addSubview:contentTableView];

    
    //尾部布局
    [self layoutFootView];

}

- (void)layoutFootView
{
    UILabel * tipLabel = [[UILabel alloc]init];
    UILabel * thingMoney = [[UILabel alloc]init];
    
    CGFloat sendAddrFont = 28 * hScale;
    tipLabel.text = [NSString stringWithFormat:@"小费:%@元",self.model.tip];
    tipLabel.frame = CGRectMake(52 * wScale, 480 *hScale, 0,0);
    tipLabel.textColor = [UIColor colorWithRed:69 green:136 blue:241];
    tipLabel.font = [UIFont systemFontOfSize:28 * hScale];
    [tipLabel sizeToFit];
    
    
    CGFloat sumMoney = 0;
    for (LRXThing * thing in self.model.thingArray) {sumMoney += [thing.thingMoney floatValue];}
    thingMoney.text = [NSString stringWithFormat:@"合计:   %.2f元",sumMoney  * 1.1];
    thingMoney.textColor = mainColor;
    thingMoney.font = [UIFont systemFontOfSize:28 * hScale];
    thingMoney.frame = CGRectMake(250 * wScale, 480 *hScale, 0,0);
    [thingMoney sizeToFit];
    
  
    CGFloat lineViewY = CGRectGetMaxY(tipLabel.frame) + 17 * hScale;
    UIImageView * lineView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Nar"]];
    lineView.frame = CGRectMake(0, lineViewY, MainScreenW - 100 * wScale, 2 * hScale);
    
    
    UILabel * sendArrLabel = [[UILabel alloc]init];
    sendArrLabel.text = self.model.sendAddr;
    sendArrLabel.font = [UIFont systemFontOfSize:sendAddrFont];
    sendArrLabel.numberOfLines = 0;
    
    CGSize sendAddrSize = [self.model.sendAddr boundingRectWithSize:CGSizeMake(360 * wScale, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:sendAddrFont]} context:nil].size;
    sendArrLabel.frame = CGRectMake(34 * wScale, lineViewY + 16 * hScale, sendAddrSize.width, sendAddrSize.height);
    

    //动态显示tableview的大小
    if ((CGRectGetMaxY(sendArrLabel.frame) + 180 * hScale) > CGRectGetMaxY(self.frame)) {
        
        self.maxHeight= CGRectGetMaxY(sendArrLabel.frame) + 20 * hScale;
    }

    //接单按钮
    UIButton * accpetBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    [accpetBtn setTitle:@"接单" forState:UIControlStateNormal];
    [accpetBtn setTitleColor:mainColor forState:UIControlStateNormal];
    accpetBtn.titleLabel.font = [UIFont systemFontOfSize:30 * hScale];
    [accpetBtn sizeToFit];
    accpetBtn.center = CGPointMake(450 * wScale, CGRectGetMaxY(lineView.frame) + (self.maxHeight - CGRectGetMaxY(lineView.frame)) * 0.5);
    [accpetBtn addTarget:self action:@selector(accpetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.accpetBtn = accpetBtn;
    
    [self addSubview:accpetBtn];
    [self addSubview:sendArrLabel];
    [self addSubview:lineView];
    [self addSubview:thingMoney];
    [self addSubview:tipLabel];

    
}
- (void)accpetBtnClick:(UIButton *)btn
{
    
    NSString * itemID = self.model.itemID;
    
    itemID = [LRXBlowFish enCodeBlowfishWithValue:itemID withKeyStr:blowFishKey];
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    NSDictionary * parm = @{@"w":itemID};
    [manger POST:acceptWaiterUrl parameters:parm progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"responseObject %@",responseObject);
            NSInteger statuCode = [responseObject[@"data"][@"status_code"] integerValue];
            
            //通知 首页删除 接单成功后的cell
            if (statuCode == 1) {
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:1];
                }
            }
            else if(statuCode == 2)
            {
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:-2];
                }
            }
            else if(statuCode == 3){
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:-3];
                }
            }
            else if(statuCode == 4){
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:-4];
                }
            }
            else if(statuCode == 5)
            {
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:-5];
                }
            }
            else if(statuCode == 6)
            {
                
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:-6];
                }
            }
            else if(statuCode == 0)
            {
                if ([self.helpMoreDelegate respondsToSelector:@selector(helpMoreInfoAccpetBtnClick:withRow:withType:)]) {
                    [self.helpMoreDelegate helpMoreInfoAccpetBtnClick:self withRow:self.row withType:0];
                }
            }
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error  %@",error);
        [MBProgressHUD showError:@"网络发生错误"];
    }];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * helpMoreReUsedID = @"helpMoreReUsedID";
    LRXHelpMoreInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:helpMoreReUsedID];
    if (cell == nil) {
        cell = [[LRXHelpMoreInfoCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:helpMoreReUsedID];
    }
    //没有边界
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    LRXThing * thing = self.model.thingArray[indexPath.row];
    [cell layoutHelpMoreCell:thing];
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.thingArray.count;
}



@end
